﻿Imports System.Data.SqlClient
Public Class sallfood
    Dim cmd As New SqlCommand
    Private Sub sallfood_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Main.lblheader.Text = "បញ្ជាលក់មុខម្ហូប"
        rdall.Checked = True
        rdtblall.Checked = True
        condb()

        'Fore Select Invoice NO
        Dim a, sum As Integer
        Dim cmde As New SqlCommand("select top(1)InvoiceID from tblinvoice order by  InvoiceID desc", cn)
        txtinvoiceNo.Text = cmde.ExecuteScalar
        a = txtinvoiceNo.Text
        sum = a + 1
        txtinvoiceNo.Text = sum

        ' for select product Name
        grdfoodlist.Rows.Clear()
        Dim cmd As New SqlCommand("select productname,price from tblproduct", cn)
        Dim rd = cmd.ExecuteReader
        While rd.Read = True
            grdfoodlist.Rows.Add(rd(0), rd(1))
        End While
        rd.Close()

        ' select tablename..
        grdtable.Rows.Clear()
        Dim cm As New SqlCommand("select tablename from tblTable", cn)
        Dim reader = cm.ExecuteReader
        While reader.Read = True
            grdtable.Rows.Add(reader(0))
        End While
        rd.Close()

    End Sub

    Private Sub grdsallfood_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdsallfood.CellContentClick

    End Sub

    Private Sub txttotaldolar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttotaldolar.TextChanged
        ' exchant to rial
        condb()
        Dim cmd As New SqlCommand("select rial from changmoney", cn)
        Dim rial = cmd.ExecuteScalar
        Dim a As Double
        a = Val(txttotaldolar.Text) * Val(rial)
        txttotalrial.Text = a
    End Sub

    Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
        sallfood_Load(sender, e)
        grdsallfood.Rows.Clear()
        txttotaldolar.Clear()
        txttotalrial.Clear()
    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        grdsallfood.Rows.Remove(grdsallfood.CurrentRow)

        Dim p As Double
        p = 0
        For i = 0 To Me.grdsallfood.Rows.Count - 1
            p = p + (Me.grdsallfood.Rows(i).Cells(2).Value * Me.grdsallfood.Rows(i).Cells(3).Value)
        Next
        txttotaldolar.Text = p
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        condb()
        Dim tablename As String
        Dim productname As String
        Dim price As Double
        Dim quality As Integer
        Dim i As Integer
        For i = 0 To grdsallfood.Rows.Count - 1
            tablename = grdsallfood.Rows(i).Cells(0).Value
            productname = grdsallfood.Rows(i).Cells(1).Value
            price = grdsallfood.Rows(i).Cells(2).Value
            quality = grdsallfood.Rows(i).Cells(3).Value

            Dim cmd As New SqlCommand("insert into tblOrderItems values(N'" & txtinvoiceNo.Text & "',N'" & tablename & "',N'" & productname & "','" & quality & "','" & price & "')", cn)
            cmd.ExecuteNonQuery()
        Next

        Dim a As String
        Dim tabename As String
        tabename = grdtable.CurrentRow.Cells("table").Value
        a = "NO"
        cmd.Connection = cn
        cmd.CommandText = "insert into tblinvoice values('" & Main.lbldate.Text & "',N'" & tabename & "','" & Main.Label1.Text & "','" & txttotaldolar.Text & "','" & a & "')"
        cmd.ExecuteNonQuery()
        MessageBox.Show("Successfull...")
    End Sub

    Private Sub t_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles t.Click
        Totalmoney.Close()
        Totalmoney.TopLevel = False
        Main.penelmain.Controls.Add(Totalmoney)
        Totalmoney.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Totalmoney.WindowState = FormWindowState.Maximized
        Totalmoney.Show()
    End Sub

    Private Sub rdtblall_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdtblall.CheckedChanged
        condb()
        grdtable.Rows.Clear()
        Dim cm As New SqlCommand("select tablename from tblTable", cn)
        Dim reader = cm.ExecuteReader
        While reader.Read = True
            grdtable.Rows.Add(reader(0))
        End While
        reader.Close()
    End Sub

    Private Sub rdprogress_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdprogress.CheckedChanged
        condb()
        grdtable.Rows.Clear()
        Dim cmd As New SqlCommand("select tableName from tblinvoice where Charge='NO'", cn)
        Dim reader = cmd.ExecuteReader
        While reader.Read = True
            grdtable.Rows.Add(reader(0))
        End While
        reader.Close()
    End Sub

    Private Sub rdall_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdall.CheckedChanged
        condb()
        grdfoodlist.Rows.Clear()
        Dim cmd As New SqlCommand("select productname,price from tblproduct", cn)
        Dim rd = cmd.ExecuteReader
        While rd.Read = True
            grdfoodlist.Rows.Add(rd(0), rd(1))
        End While
        rd.Close()
    End Sub

    Private Sub rdfood_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdfood.CheckedChanged
        condb()
        grdfoodlist.Rows.Clear()
        Dim cmd As New SqlCommand("select productname,price from tblproduct where CategoryName ='food'", cn)
        Dim rd = cmd.ExecuteReader
        While rd.Read = True
            grdfoodlist.Rows.Add(rd(0), rd(1))
        End While
        rd.Close()
    End Sub

    Private Sub rdwind_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdwind.CheckedChanged
        condb()
        grdfoodlist.Rows.Clear()
        Dim cmd As New SqlCommand("select productname,price from tblproduct where CategoryName ='wind'", cn)
        Dim rd = cmd.ExecuteReader
        While rd.Read = True
            grdfoodlist.Rows.Add(rd(0), rd(1))
        End While
        rd.Close()
    End Sub

    Private Sub Beverage_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Beverage.CheckedChanged
        condb()
        grdfoodlist.Rows.Clear()
        Dim cmd As New SqlCommand("select productname,price from tblproduct where CategoryName ='Beverage'", cn)
        Dim rd = cmd.ExecuteReader
        While rd.Read = True
            grdfoodlist.Rows.Add(rd(0), rd(1))
        End While
        rd.Close()
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        condb()
        grdfoodlist.Rows.Clear()
        Dim cmd As New SqlCommand("select productname,price from tblproduct where CategoryName ='Disert'", cn)
        Dim rd = cmd.ExecuteReader
        While rd.Read = True
            grdfoodlist.Rows.Add(rd(0), rd(1))
        End While
        rd.Close()
    End Sub

    Private Sub rdbeer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbeer.CheckedChanged
        condb()
        grdfoodlist.Rows.Clear()
        Dim cmd As New SqlCommand("select productname,price from tblproduct where CategoryName ='Beer'", cn)
        Dim rd = cmd.ExecuteReader
        While rd.Read = True
            grdfoodlist.Rows.Add(rd(0), rd(1))
        End While
        rd.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub grdfoodlist_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdfoodlist.Click
        Dim product As String
        Dim tablename As String
        Dim price As String
        product = grdfoodlist.CurrentRow.Cells("food").Value
        tablename = grdtable.CurrentRow.Cells("table").Value
        price = grdfoodlist.CurrentRow.Cells("proprice").Value
        Me.grdsallfood.Rows.Add((tablename), (product), (price), (txtqt.Text))

        'for total amount
        Dim p As Double
        p = 0
        For i As Integer = 0 To Me.grdsallfood.Rows.Count - 1
            p = p + (Me.grdsallfood.Rows(i).Cells(2).Value * Me.grdsallfood.Rows(i).Cells(3).Value)
        Next
        txttotaldolar.Text = p
    End Sub
End Class